﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Study_Time
{
    public partial class StudyTimeForm : Form
    {
        public StudyTimeForm()
        {
            InitializeComponent();
        }

        public void LoadStf(String stfFile)
        {
            studyControl.LoadStf(stfFile);
        }

        private void studyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.Mode = Program.StudyTimeMode.Study;
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.Mode = Program.StudyTimeMode.Edit;
        }

        public void ResetStudyControl()
        {
            studyControl.Reset();
        }

        public void UpdateMode()
        {
            if (Program.Mode == Program.StudyTimeMode.Study)
            {
                studyToolStripMenuItem.Checked = true;
                editToolStripMenuItem.Checked = false;
            }
            else if (Program.Mode == Program.StudyTimeMode.Edit)
            {
                studyToolStripMenuItem.Checked = false;
                editToolStripMenuItem.Checked = true;

                // Since we have entered edit mode, there may be changes that need to be saved
                studyControl.NeedsToSave = true;
            }

            PrintModeTitle();
        }

        private void PrintModeTitle()
        {
            if (Program.Mode == Program.StudyTimeMode.Study)
            {
                modeLabel.Text = "Study Mode";
            }
            else if (Program.Mode == Program.StudyTimeMode.Edit)
            {
                modeLabel.Text = "Edit Mode";
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (studyControl.FileLoaded)
                studyControl.Save();
            else
                MessageBox.Show("You are not allowed to save...nothing... :)", "Oops!", MessageBoxButtons.OK);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (studyControl.FileLoaded)
                studyControl.SaveAs();
            else
                MessageBox.Show("You are not allowed to save...nothing... :)", "Oops!", MessageBoxButtons.OK);
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            studyControl.Reset();
        }

        private void StudyTimeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (studyControl.FileLoaded && studyControl.NeedsToSave)
            {
                DialogResult result = MessageBox.Show("Do you want to save your changes?", "Oops!", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    studyControl.Save();
                else if (result == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Possibly save first
            if (studyControl.FileLoaded && studyControl.NeedsToSave)
            {
                DialogResult result = MessageBox.Show("Do you want to save your changes?", "Oops!", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    studyControl.Save();
                else if (result == DialogResult.Cancel)
                    return;
            }

            // Show the open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "JPG Files (*.jpg)|*.jpg";
            openFileDialog.Title = "Begin with a JPG File";
            openFileDialog.ShowDialog();

            // If a file was chosen
            if (!openFileDialog.FileName.Equals(""))
            {
                // Load the image file
                studyControl.LoadImage(openFileDialog.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Possibly save first
            if (studyControl.FileLoaded && studyControl.NeedsToSave)
            {
                DialogResult result = MessageBox.Show("Do you want to save your changes?", "Oops!", MessageBoxButtons.YesNoCancel);

                if (result == DialogResult.Yes)
                    studyControl.Save();
                else if (result == DialogResult.Cancel)
                    return;
            }

            // Show the open file dialog
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "STF Files (*.stf)|*.stf";
            openFileDialog.Title = "Open a Study Time File";
            openFileDialog.ShowDialog();

            // If a file was chosen
            if (!openFileDialog.FileName.Equals(""))
            {
                // Load the image file
                studyControl.LoadStf(openFileDialog.FileName);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://code.google.com/p/study-time/");
        }

        private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://code.google.com/p/study-time/downloads/list");
        }
    }
}
